/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.provider.custom;

import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.asiainfo.ability.base.service.TraceService;
import org.asiainfo.ability.base.vo.CalleeTrace;
import org.asiainfo.ability.provider.configuration.AppConfig;

import java.io.Serializable;

/**
 * @Author: visen
 * @Date: 2018/1/31
 * @Description:
 */
@Plugin(name = "EsAppender", category = "Core", elementType = "appender", printObject = true)
public class EsAppender extends AbstractAppender {
    private String head;

    private TraceService traceService;

    public EsAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, String fileName) {
        super(name, filter, layout, ignoreExceptions);
        this.head = head;
        try {
            traceService = (TraceService) AppConfig.getBean("traceService");
        } catch (Exception e) {
        }

    }

    @Override
    public void append(LogEvent event) {
        if (traceService != null) {
            final byte[] bytes = getLayout().toByteArray(event);
            RequestContext requestContext = RequestContext.getCurrentContext();
            CalleeTrace calleeTrace = (CalleeTrace) requestContext.get(RequestContext.CALLEE_TRACE);
            if (calleeTrace != null) {
                traceService.saveLogTrace(calleeTrace.getTraceId(), calleeTrace.getBizId(),
                        new String(bytes), calleeTrace.getCallId(), event.getLevel().name()
                        , System.currentTimeMillis());
            }

        }

    }


    @PluginFactory
    public static EsAppender createAppender(@PluginAttribute("name") String name,
                                            @PluginAttribute("head") String head,
                                            @PluginElement("Filter") final Filter filter,
                                            @PluginElement("Layout") Layout<? extends Serializable> layout,
                                            @PluginAttribute("ignoreExceptions") boolean ignoreExceptions) {
        if (name == null) {
            LOGGER.error("no name defined in conf.");
            return null;
        }
        if (layout == null) {
            layout = PatternLayout.createDefaultLayout();
        }

        return new EsAppender(name, filter, layout, ignoreExceptions, head);
    }


}